/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import java.util.*;
import java.io.*;

/**
 * <p>Contains the data describing a sociogram mined from the e-mail log.
 *
 * @author Andriy Nikolov
 * @version 1.0
 */
public class Sociogram {
  static final double WEIGHT_CC = 0.5;
  static final double WEIGHT_BCC = 0.5;

  double[][] weights;
  LinkedList employeesList;
  boolean useThreadsOnly = false;

  public Sociogram() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Creates a sociogram and initializes the list of nodes.
   *
   * @param employeesList LinkedList - a list of EMailRecipient objects.
   */
  public Sociogram(LinkedList employeesList) {
    this.employeesList = employeesList;
    weights = new double[employeesList.size()][employeesList.size()];
  }

  /**
   * Specifies if the sociogram will be built based on all messages or only
   * message threads (without Re:)
   *
   * @param value boolean
   */
  public void setUseThreadsOnly(boolean value) {
    useThreadsOnly = value;
  }

  public void setEmployeesList(LinkedList employeesList) {
    this.employeesList = employeesList;
    weights = new double[employeesList.size()][employeesList.size()];
  }

  public LinkedList getEmployeesList() {
    return employeesList;
  }

  /**
   * Calculates the sociogram weights based on the given set of messages.
   *
   * @param enumMessages - Enumeration of EMailMessage objects.
   */
  public void build(Enumeration enumMessages) {
    EMailMessage iterMessage;
    while(enumMessages.hasMoreElements()) {
      iterMessage = (EMailMessage)enumMessages.nextElement();
      if (iterMessage.isActive()) {
        readMessage(iterMessage);
      }
    }
    normalize();
  }

  private void readMessage(EMailMessage message) {
    EMailRecipient recipient;
    int i, j, k;
    if(useThreadsOnly) {
      if(message.getSubject().startsWith("Re:")||message.getSubject().startsWith("RE:")) {
        return;
      }
    }
    i = employeesList.indexOf(message.getFrom());
    if(i==-1)
      return;
    for (k=0;k<message.getTo().size();k++) {
      recipient = (EMailRecipient)message.getTo().get(k);
      j = employeesList.indexOf(recipient);
      if(j!=-1) {
        weights[i][j]++;
      }
    }
    for (k=0;k<message.getCc().size();k++) {
      recipient = (EMailRecipient)message.getCc().get(k);
      j = employeesList.indexOf(recipient);
      if(j!=-1) {
        weights[i][j] += WEIGHT_CC;
      }
    }
    for (k=0;k<message.getBcc().size();k++) {
      recipient = (EMailRecipient)message.getBcc().get(k);
      j = employeesList.indexOf(recipient);
      if(j!=-1) {
        weights[i][j] += WEIGHT_BCC;
      }
    }
  }

  private void normalize() {
    int i,j;
    double maxElement=1;

    for(i=0;i<employeesList.size();i++) {
      for (j=0;j<employeesList.size();j++) {
        if(weights[i][j]>maxElement) {
          maxElement = weights[i][j];
        }
      }
    }
    for(i=0;i<employeesList.size();i++) {
      for (j=0;j<employeesList.size();j++) {
        weights[i][j]/=maxElement;
      }
    }
  }

  /**
   * Returns one of the sociogram's weights.
   *
   * @param row int
   * @param col int
   * @return double
   */
  public double getWeight(int row, int col) {
    return weights[row][col];
  }

  /**
   * Saves the sociogram in the AGNA format into the writer
   *
   * @param writer PrintWriter
   */
  public void save(PrintWriter writer) {
    int i, j;
    String name;
    EMailRecipient tmp;
    writer.println("Agna Data File");
    writer.println("Network Name\tE-Mail Log Sociogram");
    writer.println("Network Size\t"+employeesList.size());
    writer.println("Has Viewer\tyes");
    writer.println("Node Names");
    for(i=0;i<employeesList.size();i++) {
      tmp = (EMailRecipient)employeesList.get(i);
      name = tmp.getName();
      if (i>0) {
        writer.print("\t");
      }
      writer.print(name);
    }
    writer.println();
    writer.println("End Node Names");
    writer.println("Network Matrix");
    for(i=0;i<employeesList.size();i++) {
      for(j=0;j<employeesList.size();j++) {
        if(j>0) {
          writer.print("\t");
        }
        writer.print(weights[i][j]);
      }
      writer.println();
    }
    writer.println("End Network Matrix");
  }

  private void jbInit() throws Exception {
  }
}
